package em.ctrl;

import base.ctrl.BaseController;
import base.jfinal.anatation.RouteBind;
import base.util.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import em.model.Eqpt;
import em.model.EqptData;
import em.model.Logger;
import em.model.Term;
import em.program.imp.OtherData;
import em.util.CommonTools;
import em.util.TermUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RouteBind(path="termManage")
public class TermManageCtrl extends BaseController {
	
	
	public void createTerm(){
		JSONArray array=JSONArray.parseArray(getPara("term"));
		int orgId = this.getParaToInt("orgId");
		if(orgId==-1){
			orgId=1;
		}
		String createDate = this.getPara("createDate");
		if(null==createDate || "".equals(createDate)){
			createDate=new SimpleDateFormat("YYYY-MM-dd").format(new Date()) ;
		}
		JSONArray jsonArr=new JSONArray();
		for (int i = 0; i < array.size(); i++) {
			Term term = (Term)Term.dao.findById(array.getString(i));
			if(term!=null){
				jsonArr.add(JSON.parseObject(String.format("{\"data\":\"终端编号:%s已存在\"}",array.getString(i))));
			}
		}

		//创建设备
		Map<String, Object> dataMap = this.getParas();
		dataMap.put("orgId", dataMap.get("GlobalOrgId"));
		dataMap.put("createDate", DateUtil.getInstance().getCurrentDate());
		dataMap.put("updateDate",DateUtil.getInstance().getCurrentDate());
		dataMap.put("type",2);

		if(jsonArr.size()>0){
			this.renderJson("{\"err\":\"1\",\"msg\":"+jsonArr.toString()+"}");
			return;
		}else{
			for (int i = 0; i < array.size(); i++) {

				dataMap.put("termCode",array.getString(i));
				dataMap.put("EqptCode",array.getString(i));
				Eqpt eqpt = new Eqpt();
				eqpt.setValues(dataMap).save();

				new EqptData().set("eqptid", eqpt.get("eqptid")).set("TermCode",array.getString(i)).set("status",0).save();

				new Term().set("TermCode", array.getString(i)).set("createDate", createDate).set("orgId", orgId).set("type",2).set("status","0").set("eqptId",eqpt.getInt("eqptId")).save();
				OtherData.getInstance().BindTerm(TermUtil.TermCodeXOR(array.getString(i)).toString(), 2, array.getString(i));
			}
			renderJson("{\"err\":0}");
		}
		
	}

    /**
     * 下发终端
     */
    public void allotTerm() {
        Db.update("UPDATE em_term SET orgid=? where termcode=? ",getPara("orgId"),getPara("termCode"));
        new Logger().set("AccountId", getParaToInt("GlobalAccId")).set("Model", "终端管理").set("Operation", "下发终端")
                .set("Desc", "终端编号："+getPara("termCode")+",下发组织:"+getPara("orgName"))
                .set("SoureIp", this.getRequest().getRemoteAddr()).save();
        renderJson("{\"err\":0}");
    }

	/**
	 * 通过终端编号向终端取得当前版本号
	 */
	public void sreachTermVersion(){
		JSONObject jsonObject = new JSONObject();
		try {
			Map map = getParas();
			Integer TNUM = TermUtil.TermCodeXOR(map.get("TermCode").toString());
			String result = OtherData.getInstance().getTermVersion(TNUM.toString());
			if (CommonTools.getInstance().notBlank(result)) {
				jsonObject=JSONObject.parseObject(result);
				if(!jsonObject.containsKey("err")){
					jsonObject.put("err",0);
				}

				jsonObject.put("HVERs",jsonObject.getString("HVER").split(",")[0]);
				jsonObject.put("SVERs",jsonObject.getString("SVER").split(",")[0]);
			}
		}catch (Exception Ex){
			jsonObject.put("err",-10);
			jsonObject.put("errMsg",Ex.getMessage());
		}

		renderJson(jsonObject);
	}
}
